<template>
	<view>
		<u-form labelPosition="left" :model="user" ref="user">
			<u-form-item label="手机号" prop="phone">
				<view class="u-demo-block__content">
					<u--input v-model="user.phone" placeholder="请输入手机号" border="surround"></u--input>
				</view>
			</u-form-item>
			<u-form-item label="验证码" prop="verifyCode">
				<view class="u-demo-block">
					<view class="u-demo-block__content">
						<u--input v-model="user.verifyCode" placeholder="请输入验证码" border="surround" password></u--input>
					</view>
				</view>
			</u-form-item>
		</u-form>
		<u-button type="success" text="获取验证码" customStyle="margin-top: 10px" @click="getCode(user.phone)"></u-button>
		<u-button type="primary" text="登录" customStyle="margin-top: 10px" @click="loginByPhone(user)"></u-button>
		<u-button text="账号密码登录" customStyle="margin-top: 10px" @click="loginByAccount"></u-button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				user: {
					phone: "",
					verifyCode: ""
				}
			}
		},
		onLoad(args){
			this.user.phone = args.userName
		},
		methods: {
			getCode(phone) {
				uni.request({
					url: "/user/login/" + phone,
					// method:'POST',
					success(res) {
						console.log(res.data.msg)

					}
				})
			},
			loginByPhone(user) {
				console.log(user)
				uni.request({
					url: "/user/loginByPhone",
					data: {
						phone: user.phone,
						verifyCode: user.verifyCode
					},
					// method:'POST',
					success(res) {
						if (res.data.code == 200) {
							// 保存 token
							uni.setStorageSync('token', res.data.data);
							//跳到首页
							uni.switchTab({
								url: "/pages/index/index"
							})

						} else {
							console.log(res.data.msg)
						}
					}
				})
			},
			loginByAccount() {
				uni.redirectTo({
					url: "/pages/loginByAccount"
				})
			}
		}
	}
</script>

<style>
</style>